//
//  HTRegisterTruckOtherVC.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/28.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTRegisterTruckOtherVC.h"
#import "HTTruckLicenseView.h"
#import "HTMineViewController.h"


@interface HTRegisterTruckOtherVC ()<UITextFieldDelegate,HTTruckLicenseViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *tapYellow;
@property (weak, nonatomic) IBOutlet UIView *tapBlue;
@property (weak, nonatomic) IBOutlet UIImageView *tipYelImageView;
@property (weak, nonatomic) IBOutlet UIImageView *tipBluImageView;

@property (nonatomic,strong) NSString *cardType;
@property (nonatomic,strong) NSString *license;
@property (weak, nonatomic) IBOutlet UITextField *mainTextField;
@property (weak, nonatomic) IBOutlet UIButton *commitButton;
@property (nonatomic,assign) NSInteger regist;
@end

@implementation HTRegisterTruckOtherVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请副驾";
    [self initData];
    [self netLoadUserMessage];
}
- (void)initData{
    self.regist = self.mineModel ? [self.mineModel.regist integerValue] : 0;
    if (self.regist == 2 || self.regist == 3) {//不能修改
        self.mainTextField.userInteractionEnabled = NO;
        self.mainTextField.text = self.mineModel.carids;
        if ([self.mineModel.numbertype isEqualToString:@"1"]) {//numbertype 车牌颜色 车牌类型 1 蓝 2 黄
            self.tipBluImageView.image = [UIImage imageNamed:@"icon_blue_checked_circle"];
            self.tipYelImageView.image = [UIImage imageNamed:@"icon_blue_circle"];
        }else if ([self.mineModel.numbertype isEqualToString:@"2"]){
            self.tipBluImageView.image = [UIImage imageNamed:@"icon_blue_circle"];
            self.tipYelImageView.image = [UIImage imageNamed:@"icon_blue_checked_circle"];
        }
        self.cardType = self.mineModel.numbertype;
        self.license = self.mineModel.carids;
        if (self.regist == 2) {
            self.commitButton.hidden = YES;
        }
        if (self.regist == 3) {
            self.commitButton.hidden = NO;
        }
    }else if(self.regist == 0){
        self.mainTextField.delegate = self;
        self.mainTextField.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didCallLisence:)];
        [self.mainTextField addGestureRecognizer:tap];
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapTruckType:)];
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapTruckType:)];
        self.tapBlue.tag = 141;
        self.tapYellow.tag = 142;
        [self.tapBlue addGestureRecognizer:tap1];
        [self.tapYellow addGestureRecognizer:tap2];
        self.commitButton.hidden = NO;
    }
}

- (void)didCallLisence:(id)sender{
    HTTruckLicenseView *truckLicenseView = [[HTTruckLicenseView alloc]init];
    truckLicenseView.delegate = self;
    [truckLicenseView showView];
}
- (void)didGotCallBackLicense:(NSString *)title{
    self.mainTextField.text = title;
    self.license = title;
}
- (void)didTapTruckType:(UITapGestureRecognizer *)sender{
    if (sender.view.tag == 141) {//blue
        self.tipBluImageView.image = [UIImage imageNamed:@"icon_blue_checked_circle"];
        self.tipYelImageView.image = [UIImage imageNamed:@"icon_blue_circle"];
        self.cardType = @"1";
    }else{//yellow
        self.tipBluImageView.image = [UIImage imageNamed:@"icon_blue_circle"];
        self.tipYelImageView.image = [UIImage imageNamed:@"icon_blue_checked_circle"];
        self.cardType = @"2";
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}
- (IBAction)didClickCommit:(id)sender {
    if (self.name == nil || self.name.length <= 0) {
        [HTShowUtil showMessage:@"请输入姓名"];
        return;
    }
    if (self.idCard == nil || self.idCard.length <= 0) {
        [HTShowUtil showMessage:@"请输入身份证号"];
        return;
    }
    if (self.cardType == nil || self.cardType.length <= 0) {
        [HTShowUtil showMessage:@"请选择车牌类型"];
        return;
    }
    if (self.license == nil || self.license.length <= 0) {
        [HTShowUtil showMessage:@"请输入车牌号码"];
        return;
    }
    [self netLoadRegister];
}

#pragma mark -- 网络请求
- (void)netLoadRegister{
    [self startHud];
    [HTHttpRequestManager requestRegisterWithDrivercar:self.idCard username:self.name cards:self.idCard ismast:@"1" cartype:self.cardType  carnumber:self.license carwm:@"" carlength:@"" goas:@"" success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"注册：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"注册失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [UIAlertController alertStyleShowInController:self title:@"" message:responseObject[@"message"] alertActionsBlock:^(NSMutableArray<UIAlertAction *> *alertActions) {
                [alertActions addObject:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
            } completion:nil];
            return;
        }
        [HTShowUtil showMessage:@"注册成功"];
        for (UIViewController * controller in self.navigationController.viewControllers) {
            if ([controller isKindOfClass:[HTMineViewController class]]) {
                [self.navigationController popToViewController:controller animated:YES];
                return;
            }
        }
        [self.navigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"注册失败"];
    }];
}
- (void)netLoadUserMessage{
    [self startHud];
    [HTHttpRequestManager requestUserMessageSuccess:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"个人信息：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"获取失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:@"获取失败"];
            return;
        }
        self.mineModel = [HTMineModel mj_objectWithKeyValues:responseObject[@"data"]];
        [self initData];
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"获取失败"];
    }];
}
@end
